<script setup>
import { ref, defineProps, reactive } from 'vue'
import { _searchItem, _saveItem } from '/src/api/parameterConfiguration/index.js'
import { pushAllUrlData } from '../js/saveItem_all.js'
import { mainStore } from '/src/store/mainStore.js'

const store = mainStore()
const msg = defineProps({
  data: Object,
  width: Number,
})

const showType = ref('') //显示类型
const children = ref([]) //下拉选择框的子选项
const width = ref(0)
children.value = msg.data.children
showType.value = msg.data.showType
width.value = msg.width ? msg.width + 'px' : '100%'

const editStatus = ref('') // 编辑状态 未保存为true 保存为faslse

const errTip = ref(null)

const verifyInput = () => {
  pushAllUrlData(msg.data)
}

const addItem = () => {
  msg.data.arr.push(JSON.parse(JSON.stringify(msg.data.arr[0])))
  pushAllUrlData(msg.data)
}

// 搜索选项
const searchItem = async () => {
  const { parentNode, key } = msg.data

  const url = `${store.cutArgPage}${parentNode ? '/' + parentNode : ''}/${key}`
  const res = await _searchItem(url)
  if (res.data.code != 200) ElMessage.error('查询失败')

  ElMessage.success('查询成功')
  msg.data.arr = res.data.data[key]

  editStatus.value = ''
  errTip.value = null
}

// 保存选项
const saveItem = async () => {
  if (errTip.value != null) return ElMessage.error('保存失败，请输入正确的参数值')

  const { parentNode, key } = msg.data

  const params = {}
  if (!params[parentNode]) params[parentNode] = {}
  params[parentNode][key] = msg.data.arr
  const url = `${store.cutArgPage}`

  const res = await _saveItem(url, params)

  if (res.data.code != 200) ElMessage.error('保存失败')
  ElMessage.success('保存成功')
  msg.data.arr = res.data.data[parentNode][key]

  editStatus.value = ''
  errTip.value = null
}

const deleteItem = (index) => {
  msg.data.arr.splice(index, 1)
  pushAllUrlData(msg.data)
}
</script>

<template>
  <div class="inputEditerArr-container">
    <div class="title">
      <p :class="editStatus ? 'editing' : ''">
        <span>{{ editStatus }}</span>
        {{ msg.data.name }}
      </p>
      <img src="/public/parameterConfig/add.png" @click="addItem" />
      <img src="/public/parameterConfig/search.png" @click="searchItem" />
      <img src="/public/parameterConfig/save.png" @click="saveItem" />
    </div>

    <div class="ctrlBox">
      <!-- 循环的项 -->
      <div class="item" v-for="(item, index) in msg.data.arr">
        <div class="inputBox" v-for="(value, key) in item">
          <p>{{ key }}_{{ index }}</p>
          <el-input v-model.number="item[key]" clearable @input="verifyInput" style="width: 500px" />
          <img src="/public/parameterConfig/delete.png" @click="deleteItem(index)" style="margin-left: 10px" />
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.inputEditerArr-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  color: black;
  border: 1px solid #ccc;
  padding: 10px;
  > p {
    line-height: 26px;
  }
  img {
    width: 20px;
    height: 20px;
    margin-left: 4px;
    cursor: pointer;
  }

  .editing {
    color: blue;
  }

  .ctrlBox {
    display: flex;
    height: 200px;
    flex-direction: column;
    overflow-y: auto;
    transition: width 0.3s ease;
    margin-top: 10px;
  }

  .item {
    // width: 360px;
    min-height: 30px;
    overflow-y: auto;
    padding: 0 10px;

    margin: 0 20px;
    flex-shrink: 0;
    .inputBox {
      display: flex;
      align-items: center;
      margin-top: 16px;

      > p {
        width: 60px;
        font-size: 14px;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        img {
          line-height: 20px;
          width: 14px;
          height: 14px;
          margin-top: 4px;
        }
      }
    }

    .arr2Box {
      margin-bottom: 10px;
      border-bottom: 1px solid #ccc;
      padding: 0 0 6px 0;
    }

    .arrBox {
      width: 100%;
      height: 200px;
      border: 1px solid #ccc;
      overflow-y: auto;
      font-size: 12px;
      padding: 10px;
      img {
        width: 12px;
        height: 12px;
        margin-top: 2px;
      }
      .arrBoxItem {
        display: flex;
        margin-top: 4px;
        align-items: center;
        > p {
          width: 100px;
        }
      }
    }
  }

  .title {
    display: flex;
    align-items: center;
    > img {
      margin-left: 10px;
      width: 20px;
      height: 20px;
    }
  }
  .title2 {
    display: flex;
    align-items: center;
    > img {
      margin-left: 10px;
      width: 16px;
      height: 16px;
    }
  }

  .errorTip {
    position: absolute;
    left: 0px;
    top: 26px;
    font-size: 10px;
    color: red;
    padding-left: 6px;
    width: 100%;
    opacity: 0.8;
  }
}
</style>

<style lang="less">
.inputBox,
.selectBox {
  .el-input,
  .el-input--suffix {
    height: 26px;
  }
  .el-input__inner,
  .el-input__wrapper {
    height: 26px;
  }

  .el-select__wrapper {
    height: 26px;
    min-height: 26px;
  }
}

.arrBoxItem {
  .el-input,
  .el-input--suffix {
    height: 20px;
  }
  .el-input__inner,
  .el-input__wrapper {
    height: 20px;
  }

  .el-select__wrapper {
    height: 20px;
    min-height: 20px;
  }
}
</style>
